<?php
namespace app\parent;
use think\Controller;
use think\Db;
use think\facade\Session;

class api extends Controller
{
    public $w;
    public $a;
    public $u;

    protected $beforeActionList = [
        'first',
    ];
    
    protected function first()
    {
        // if($_SERVER['HTTP_HOST'] != get_default('web_domain')){
        //     $this->ajaxReturn(400,'域名错误');
        //     die;
        // }

        $login_arr = [
            'index/login', // 登陆
            'index/XLogin', // 登陆
        ];
        if(!in_array($this->request->path(),$login_arr)){
            if(request()->has('token','post')){
                $this->token();
            }
        }
    }

    public $request;
    public $session;

	public function _initialize()
    {
        // ->fetchSql(true)
        $this->request  =   request();

    }

    // json 返回
    public function ajaxReturn($code = '',$msg = '',$data = '',$count = 0)
    {
        echo json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data,'count'=>$count]);die;
    }

    public function token()
    {
        $token = $this->input('token',0);
        if(!$token){
            return $this->ajaxReturn(400,'缺少必要参数！');
        }

        // 0 = wid
        // 1 = aid
        // 2 = uid
        $tokens = explode(',',base64_decode($token));
        if(count($tokens) < 2){
            return $this->ajaxReturn(400,'缺少参数错误！');
        }

        $wid = $tokens[0];
        $aid = $tokens[1];

        $this->w = Db::name('agency_user')->where('id',$wid)->find();
        if(!$this->w){
            $this->ajaxReturn(420,'该项目暂时停止！');
            die;
        }

        if($this->w['agency_user_is_show'] != 1){
            $this->ajaxReturn(421,'该项目暂时停止2！');
            die;
        }

        if($this->w['agency_user_check'] < 2){
            $this->ajaxReturn(423,'该项目正在审核，请等待！');
            die;
        }

        $this->a = Db::name('app')->where('id',$aid)->find();
        if(!$this->a){
            $this->ajaxReturn(410,'该项目暂时停止！');
            die;
        }

        if($this->a['app_is_show'] != 1){
            $this->ajaxReturn(411,'该项目暂时停止2！');
            die;
        }

        if($this->a['app_is_check'] < 3){
            $this->ajaxReturn(413,'该项目正在审核，请等待！');
            die;
        }

        if(isset($tokens[2]) && $tokens[2] > 0){
            $uid = $tokens[2];
            $this->u = Db::name('user')->where('id',$uid)->find();
            if(!$this->u){
                $this->ajaxReturn(430,'P用户信息未找到！');
                die;
            }

            if($this->u['user_is_show'] != 1){
                $this->ajaxReturn(431,'P您已被暂时屏蔽！');
                die;
            }

            $this->sessionSet('token',$token);
        }else{
            $this->sessionSet('wd_token',$token);
        }

    }


    /**
    * showdoc
    * 操作日志
    * event  事件类型
    * content  事件内容
    */
    public function setLog($event = '',$content = '')
    {
        \think\Db::name('admin_log')->insert([
            'admin_log_admin_name'  => session('admin_user_nickname'),
            'admin_log_admin_ip'    => request()->ip(),
            'admin_log_rank_name'   => session('admin_rank_title'),
            'admin_log_is_show'     => 1,
            'admin_log_times'       => time(),
            'admin_log_event'       => $event,
            'admin_log_content'     => $content,
        ]);
    }

    // 是否为Post访问
    public function isPost()
    {
        return $this->request->isPost();
    }

    // 是否为Get访问
    public function isGet()
    {
        return $this->request->isGet();
    }

    // 是否为Ajax访问
    public function isAjax()
    {
        return $this->request->isAjax();
    }

    // 是否为手机访问
    public function isMobile()
    {
        return $this->request->isMobile();
    }


    // 获取哪些请求参数
    public function input($data = '',$redata = false)
    {

        if($data){
            if(is_array($data)){
                $arr = [];
                $param = $this->request->param();
                foreach ($data as $key => $value) {

                    if(isset($param[$value])){
                        $arr[$value] = $param[$value];
                    }else{
                        if($redata !== false){
                            $arr[$value] = $redata;
                        }
                    }

                }
                return $arr;
            }else{

                $arr = $redata;
                $param = $this->request->param();
                foreach ($param as $key => $value) {

                    if($key == $data){
                        $arr = $value;
                    }

                }

                return $arr;
            }
        }else{
            $param = $this->request->param();
            if(isset($param['adminrankid'])){
                unset($param['adminrankid']);
            }

            if(isset($param['adminrankkey'])){
                unset($param['adminrankkey']);
            }
            return $param;
        } 
    }

    // 获取哪些请求参数
    public function unInput($data = '')
    {
        if($data){
            if(is_array($data)){
                
                $data[] = 'adminrankid';
                $data[] = 'adminrankkey';
                
                $param = $this->request->param();
                foreach ($param as $key => $value) {
                    if(in_array($key,$data)){
                        unset($param[$key]);
                    }
                }
                return $param;
            
            }else{
                $param = $this->request->param();

                if(isset($param[$data])){
                    unset($param[$data]);
                }

                if(isset($param['adminrankid'])){
                    unset($param['adminrankid']);
                }
    
                if(isset($param['adminrankkey'])){
                    unset($param['adminrankkey']);
                }

                return $param;
            }
        }else{
            $param = $this->request->param();
            if(isset($param['adminrankid'])){
                unset($param['adminrankid']);
            }

            if(isset($param['adminrankkey'])){
                unset($param['adminrankkey']);
            }
            return $param;
        }
    }

    public function session($data = '',$reData = '')
    {
        if(is_array($data) && $data){

            $arr = [];
            foreach ($data as $k => $v) {
                if(Session::has($v)){
                    $arr[$v] = Session::get($v);
                }else{
                    $arr[$v] = $reData;
                }
            }
            return $arr;

        }else if($data){

            if(Session::has($data)){
                return Session::get($data);
            }else{
                return $reData;
            }

        }else{

            return $reData;

        }
    }

    public function sessionSet($data = '',$reData = '')
    {
        if($data){
            if(is_array($data)){
                foreach ($data as $key => $value) {
                    Session::set($key,$value);
                }
            }else{
                Session::set($data,$reData);
            }
        }

        return '';
    }

}
